package top.wuxiaohan.operatingSystemCourse.kernel;

/**
 * 进程树节点，存放父进程指针和子进程指针
 */
public class TreeNode {
    /**
     * 父进程节点
     */
    private TreeNode parentProcess;
    /**
     * 子进程节点
     */
    private TreeNode childProcess;

    public TreeNode() {

        this.parentProcess = null;
        this.childProcess = null;
    }

    public TreeNode(TreeNode parentProcess) {

        this.parentProcess = parentProcess;
        this.childProcess = null;
    }


    public void setParentProcess(TreeNode parentProcess) {
        this.parentProcess = parentProcess;
    }

    /**
     * 添加子进程节点：将该节点插入至子进程链表末端
     */
    public void setChildProcess(TreeNode childProcess) {

        //TODO 这段代码尚未经过测试，且插入节点到末尾还是前面好一点呢？
        TreeNode lastNode = this.childProcess;
        while (lastNode != null) {
            //遍历链表，找到最末尾的元素
            lastNode = lastNode.childProcess;
        }

        lastNode.childProcess = childProcess;
    }

    /**
     * 获取父进程节点
     */
    public TreeNode getParentProcess() {
        return parentProcess;
    }

    /**
     * 获取子进程节点
     */
    public TreeNode getChildProcess() {
        return childProcess;
    }

}
